#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'libb@anchent.com'

import os
import time
from random import randint
from multiprocessing import Pool
import requests
import json

# 压测平台推流工具服务ip地址
LOAD_PLATFORM = "9.218.27.137:8080"


def download_task(name):
    print("current task parent pid:%d, child pid:%s" % (os.getppid(), os.getpid()))
    time_to_download = randint(5, 10)
    time.sleep(time_to_download)
    print("download_task file %s total cost time:%d" % (name, time_to_download))


def get_account_list(index, appid="1400143280"):
    url = "http://{}/api/v1/get_account_id".format(LOAD_PLATFORM)
    params = {
        "appid": appid
    }
    headers = {
        "Content-Type": "application/json"
    }
    res = requests.post(url, data=json.dumps(params), headers=headers)
    rsp_data = res.content
    if res.status_code == 201:
        rsp_info = json.loads(rsp_data)
        account_id = rsp_info.get("data").get("account_id")
        print "account_id:{}, index:{}".format(account_id, index)
        return account_id
    else:
        print "get_account_list failed  url:{}, params:{}, rsp_data:{}".format(url, params, rsp_data)
        return None


if __name__ == '__main__':
    start = time.time()
    total_download_time = 0
    print("parent pid is:%d" % os.getpid())
    p = Pool(processes=2)
    for i in range(10):
        p.apply_async(get_account_list, args=(i,))
    print("wait for all process done")
    p.close()
    p.join()
    end = time.time()
    print("python pool total cost time %.2f, %d" % (end - start, total_download_time))
